<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Contour Properties</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d6/d00/tutorial_py_root.html">OpenCV-Python Tutorials</a></li><li class="navelem"><a class="el" href="../../d2/d96/tutorial_py_table_of_contents_imgproc.html">Image Processing in OpenCV</a></li><li class="navelem"><a class="el" href="../../d3/d05/tutorial_py_table_of_contents_contours.html">Contours in OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Contour Properties </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Here we will learn to extract some frequently used properties of objects like Solidity, Equivalent Diameter, Mask image, Mean Intensity etc. More features can be found at <a href="http://www.mathworks.in/help/images/ref/regionprops.html">Matlab regionprops documentation</a>.</p>
<p>*(NB : Centroid, Area, Perimeter etc also belong to this category, but we have seen it in last chapter)*</p>
<h2>1. Aspect Ratio </h2>
<p>It is the ratio of width to height of bounding rect of the object.</p>
<p class="formulaDsp">
\[Aspect \; Ratio = \frac{Width}{Height}\]
</p>
 <div class="fragment"><div class="line">x,y,w,h = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga103fcbda2f540f3ef1c042d6a9b35ac7">cv.boundingRect</a>(cnt)</div><div class="line">aspect_ratio = float(w)/h</div></div><!-- fragment --><h2>2. Extent </h2>
<p>Extent is the ratio of contour area to bounding rectangle area.</p>
<p class="formulaDsp">
\[Extent = \frac{Object \; Area}{Bounding \; Rectangle \; Area}\]
</p>
 <div class="fragment"><div class="line">area = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1">cv.contourArea</a>(cnt)</div><div class="line">x,y,w,h = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga103fcbda2f540f3ef1c042d6a9b35ac7">cv.boundingRect</a>(cnt)</div><div class="line">rect_area = w*h</div><div class="line">extent = float(area)/rect_area</div></div><!-- fragment --><h2>3. Solidity </h2>
<p>Solidity is the ratio of contour area to its convex hull area.</p>
<p class="formulaDsp">
\[Solidity = \frac{Contour \; Area}{Convex \; Hull \; Area}\]
</p>
 <div class="fragment"><div class="line">area = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1">cv.contourArea</a>(cnt)</div><div class="line">hull = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656">cv.convexHull</a>(cnt)</div><div class="line">hull_area = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1">cv.contourArea</a>(hull)</div><div class="line">solidity = float(area)/hull_area</div></div><!-- fragment --><h2>4. Equivalent Diameter </h2>
<p>Equivalent Diameter is the diameter of the circle whose area is same as the contour area.</p>
<p class="formulaDsp">
\[Equivalent \; Diameter = \sqrt{\frac{4 \times Contour \; Area}{\pi}}\]
</p>
 <div class="fragment"><div class="line">area = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1">cv.contourArea</a>(cnt)</div><div class="line">equi_diameter = np.sqrt(4*area/np.pi)</div></div><!-- fragment --><h2>5. Orientation </h2>
<p>Orientation is the angle at which object is directed. Following method also gives the Major Axis and Minor Axis lengths. </p><div class="fragment"><div class="line">(x,y),(MA,ma),angle = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gaf259efaad93098103d6c27b9e4900ffa">cv.fitEllipse</a>(cnt)</div></div><!-- fragment --><h2>6. Mask and Pixel Points </h2>
<p>In some cases, we may need all the points which comprises that object. It can be done as follows: </p><div class="fragment"><div class="line">mask = np.zeros(imgray.shape,np.uint8)</div><div class="line"><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc">cv.drawContours</a>(mask,[cnt],0,255,-1)</div><div class="line">pixelpoints = np.transpose(np.nonzero(mask))</div><div class="line"><span class="comment">#pixelpoints = cv.findNonZero(mask)</span></div></div><!-- fragment --><p> Here, two methods, one using Numpy functions, next one using OpenCV function (last commented line) are given to do the same. Results are also same, but with a slight difference. Numpy gives coordinates in **(row, column)** format, while OpenCV gives coordinates in **(x,y)** format. So basically the answers will be interchanged. Note that, <b>row = y</b> and <b>column = x</b>.</p>
<h2>7. Maximum Value, Minimum Value and their locations </h2>
<p>We can find these parameters using a mask image. </p><div class="fragment"><div class="line">min_val, max_val, min_loc, max_loc = <a class="code" href="../../d2/de8/group__core__array.html#ga8873b86a29c5af51cafdcee82f8150a7">cv.minMaxLoc</a>(imgray,mask = mask)</div></div><!-- fragment --><h2>8. Mean Color or Mean Intensity </h2>
<p>Here, we can find the average color of an object. Or it can be average intensity of the object in grayscale mode. We again use the same mask to do it. </p><div class="fragment"><div class="line">mean_val = <a class="code" href="../../d2/de8/group__core__array.html#ga191389f8a0e58180bb13a727782cd461">cv.mean</a>(im,mask = mask)</div></div><!-- fragment --><h2>9. Extreme Points </h2>
<p>Extreme Points means topmost, bottommost, rightmost and leftmost points of the object. </p><div class="fragment"><div class="line">leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])</div><div class="line">rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])</div><div class="line">topmost = tuple(cnt[cnt[:,:,1].argmin()][0])</div><div class="line">bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])</div></div><!-- fragment --><p> For eg, if I apply it to an Indian map, I get the following result :</p>
<div class="image">
<img src="../../extremepoints.jpg" alt="extremepoints.jpg"/>
<div class="caption">
image</div></div>
 <h2>Additional Resources </h2>
<h2>Exercises </h2>
<ol type="1">
<li>There are still some features left in matlab regionprops doc. Try to implement them. </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
